/*
 LegislatorListViewController.m
 LegislatorLookup
 
 This class controls the Legislator List view which lists
 all the legislators for a given state
 
 Copyright (c) 2009 Nabil Ahmad
 All rights reserved.
 
 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions
 are met:
 
 1. Redistributions of source code must retain the above copyright
 notice, this list of conditions and the following disclaimer.
 
 2. Redistributions in binary form must reproduce the above copyright
 notice, this list of conditions and the following disclaimer in the 
 documentation and/or other materials provided with the distribution. 
 
 3. The name of the author may not be used to endorse or promote products
 derived from this software without specific prior written permission.
 
 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 
 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */	

#import "LegislatorListViewController.h"
#import "WebServiceController.h"
#import "Legislator.h"
#import "LegislatorViewController.h"

@implementation LegislatorListViewController

- (id)initWithState:(NSString *)selectedState 
{
	[super init];
	state = selectedState;
	
	return self;
}

- (void)loadView 
{
	[super loadView];
	isDataLoaded = NO;
}

// load data and rerender view after view is loaded
- (void)viewDidAppear:(BOOL)animated
{
	// Load data
	legislators = [[WebServiceController getLegislatorsForState:state] retain];
	isDataLoaded = YES;
	
	// If there are no senators/reps, there must be a data error
	if ([[legislators objectAtIndex:0] count] == 0 && [[legislators objectAtIndex:1] count] == 0)
		dataError = YES;
	
	[self.tableView reloadData];
}

- (void)didReceiveMemoryWarning 
{
    [super didReceiveMemoryWarning]; 
}

// Return sections for reps and for senators
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{
	int sections = 1;
	if (!dataError && isDataLoaded)
		sections = 2;
	
	return sections;
}

// Return count of each type
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{
	int count = 0;
	if (!dataError && isDataLoaded)
		count = [[legislators objectAtIndex:section] count];
	else if (dataError)
		count = 1;
	
	return count;
}

// Return cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }
	if (!dataError)
	{
		cell.text = [[[legislators objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] displayName];  
		cell.image = [[[legislators objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] partyIcon];  
	}
	else
	{
		cell.text = @"There has been a data access error. Please try again later.";
		cell.font = [UIFont boldSystemFontOfSize:10.0];
	}
	
    return cell;
}

// On row selection take user to Legislator View
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
	if (!dataError)
	{
		LegislatorViewController *legislatorViewController = [[LegislatorViewController alloc] initWithLegislator:[[legislators objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]];
	
		[self.navigationController pushViewController:legislatorViewController animated:YES];
		[legislatorViewController release];
	}
}

- (UITableViewCellAccessoryType)tableView:(UITableView *)table accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath 
{
	if (!dataError)
		return UITableViewCellAccessoryDisclosureIndicator;
	else
		return UITableViewCellAccessoryNone;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{
	NSString *header = @"";

	if (!dataError && isDataLoaded)
	{
		if (section == 0)
			header = @"Senators";
		else
			header = @"Represenatives";
	}
	
	return header;
}

- (void)dealloc {
    [super dealloc];
	
	[legislators release];
}


@end

